﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using NUnit.Framework;

namespace CreateAProgrammingPearl
{
	[TestFixture]
	public class Code
	{
		[Test]
		public void Input_with_bad_characters_should_be_replaced_with_approprate_character()
		{
			var badChars = new[] {(char) 96, (char) 8216, (char) 8217, (char) 8242, (char) 769, (char) 768,
			                      (char) 8220, (char) 8221, (char) 8243, (char) 12291,(char)8208, (char)8211, (char)8212, 
			                      (char)8722, (char)173, (char)8209, (char)8259,(char)160, (char)8195, (char)8194};
			var badString = new string(badChars);
  
			var cleanedString = CleanInput(badString);
     
			Assert.That(cleanedString, Is.EqualTo("''''''\"\"\"\"-------   "));   
		}

		const char Hyphen = (char)45;
		const char SingleQuote = (char)39;
		const char DoubleQuote = (char)34;
		const char Space = (char)32;
		private static readonly IDictionary<char, char> Replacements = new Dictionary<char, char>
								{
									{(char)8208, Hyphen}, 
									{(char)8211, Hyphen}, 
									{(char)8212, Hyphen}, 
									{(char)8722, Hyphen}, 
									{(char)173, Hyphen}, 
									{(char)8209, Hyphen}, 
									{(char)8259, Hyphen}, 
									{(char)96, SingleQuote}, 
									{(char)8216, SingleQuote}, 
									{(char)8217, SingleQuote}, 
									{(char)8242, SingleQuote}, 
									{(char)769, SingleQuote}, 
									{(char)768, SingleQuote},
									{(char)8220, DoubleQuote},
									{(char)8221, DoubleQuote},
									{(char)8243, DoubleQuote}, 
									{(char)12291, DoubleQuote},
									{(char)160, Space}, 
									{(char)8195, Space}, 
									{(char)8194, Space}
								};
		static readonly string Pattern = string.Concat("([", string.Concat(Replacements.Select(r => r.Key.ToString()).ToArray()), "])");

		public static string CleanInput(string input)
		{
			return Regex.Replace(input, Pattern, match => Replacements[Convert.ToChar(match.Value)].ToString(), RegexOptions.Compiled);
		}
	}
}